#include "curl_http_client.h"
#include "curl_response.h"

#include <chrono>
#include <curl/curl.h>
#include <iostream>
#include <json/value.h>
#include <thread>
using namespace std;

#define GLOG_USE_GLOG_EXPORT
#include <glog/logging.h>

std::string get_json_data()
{
    Json::Value root;
    root["version"] = 1;
    root["message"] = "hello";
    return root.toStyledString();
}

void callback(const char *contents, size_t size, void *stream)
{
    std::string *str = static_cast<std::string *>(stream);
    *str             = std::string((char *)contents);
    std::cout << *str << std::endl;
}

void test_getMethod()
{
    CurlHttpClient client;
    auto           headers = client.createHeaders({"content-type:application/json"});
    CurlResponse   response;
    if (client.getMethod(response, "http://127.0.0.1:10010/hello", headers.get())) {
        LOG(INFO) << "Request successful, response: \n" << response.response;
    } else {
        LOG(INFO) << "Request failed, errmsg: \n" << response.errmsg;
    }
}

void test_postMethod()
{
    CurlHttpClient client;
    auto           headers = client.createHeaders({"content-type:application/json"});
    CurlResponse   response;
    std::string    strRequest = get_json_data();
    if (client.postMethod(response, "http://127.0.0.1:10010/hello", headers.get(), strRequest)) {
        LOG(INFO) << "Request successful, response: \n" << response.response;
    } else {
        LOG(INFO) << "Request failed, errmsg: \n" << response.errmsg;
    }
}

void test_getMethodMultipart()
{
    CurlHttpClient client;
    std::string    errmsg;
    CurlResponse   response;
    auto           headers = client.createHeaders({"content-type:application/json"});

    auto custom_headers = client.createHeaders({});
    if (client.getMethodMultipart("http://127.0.0.1:10010/multipart",
                                  headers.get(),
                                  callback,
                                  (void *)&response.response,
                                  errmsg,
                                  CURLAUTH_DIGEST,
                                  "admin:admin123456")) {
        LOG(INFO) << "Request successful, response: \n" << response.response;
    } else {
        LOG(INFO) << "Request failed, errmsg: \n" << response.errmsg;
    }
}

int main(int argc, char *argv[])
{
    test_getMethodMultipart();
    return 0;
}
